﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

namespace X23.Util.LinqUtil
{

    public static class DynamicUtil
    {
        /// <summary>
        /// 高级排序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="advancedSortList"></param>
        /// <returns></returns>
        public static IOrderedQueryable<T> AdvancedSort<T>(this IQueryable<T> query, List<AdvancedSort> advancedSortList)
        {
            if (advancedSortList == null || advancedSortList.Count == 0)
            {
                throw new Exception("未包含排序字段！");
            }
            if (advancedSortList.Any(x => string.IsNullOrEmpty(x.SortColumn) || !(x.SortType == 0 || x.SortType == 1)))
            {
                throw new Exception("排序字段不正确！");
            }
            var sortArr = advancedSortList.Select(x => $"{x.SortColumn} {x.SortTypeStr}").ToArray();

            return query.DynamicOrderBy(sortArr);
        }

        public static IOrderedQueryable<T> DynamicOrderBy<T>(this IQueryable<T> query, params string[] sortArr)
        {
            if (sortArr.Length == 0)
            {
                throw new Exception("未包含排序字段！");
            }
            IOrderedQueryable<T> result = null;
            for (int i = 0; i < sortArr.Length; i++)
            {
                var item = sortArr[i];
                if (i == 0)
                {
                    result = query.OrderBy(item);
                }
                else
                {
                    result = result.DynamicThenBy(item);
                }
            }
            return result;
        }
        public static IOrderedQueryable<T> DynamicThenBy<T>(this IOrderedQueryable<T> orderedQueryable, params string[] sortArr)
        {
            if (sortArr.Length == 0)
            {
                throw new Exception("未包含排序字段！");
            }
            IOrderedQueryable<T> result = orderedQueryable;
            for (int i = 0; i < sortArr.Length; i++)
            {
                result = result.ThenBy(sortArr[i]);
            }
            return result;
        }
    }



    public interface IAdvancedSortDto
    {
        List<AdvancedSort> AdvancedSort { get; set; }
    }
    /// <summary>
    /// 高级排序
    /// </summary>
    public class AdvancedSortDto : IAdvancedSortDto
    {
        /// <summary>
        /// 排序列表
        /// </summary>
        public List<AdvancedSort> AdvancedSort { get; set; }
    }
    /// <summary>
    /// 高级排序
    /// </summary>
    public class AdvancedSort
    {
        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortColumn { get; set; }
        /// <summary>
        /// 排序类型 0为降序 1为升序
        /// </summary>
        public int SortType { get; set; }
        /// <summary>
        /// 排序类型 (只读)
        /// </summary>
        public string SortTypeStr => SortType == 0 ? "DESC" : "ASC";
    }
}
